package com.tgpms.web.earlierStage.controller;

import com.alibaba.fastjson.JSON;
import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.UsersAuthority;
import com.tgpms.web.earlierStage.service.UsersAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName UsersAuthorityController
 * @Description TODO
 * @Author GUAN
 * @Date 2019-12-24 16:24
 * @Version 1.0
 * @return
 **/
@Api(value = "UsersAuthorityController", tags = "前期 用户权限")
@RestController
@RequestMapping(value = "/ev/usersAuthority")
public class UsersAuthorityController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UsersAuthorityService usersAuthorityService;

    @ApiOperation(value = "批量新增用户权限接口", notes = "新增用户权限接口", httpMethod = "POST")
    @PostMapping(value = "/addMultiUsersAuthority")
    public Result addMultiUsersAuthority(@RequestParam(value = "usersId", required = true) String usersId,
                                         @RequestParam(value = "userName", required = true) String userName,
                                         @RequestParam(value = "adminYn", required = true) String adminYn,
                                         @RequestParam(value = "checkYn", required = true) String checkYn,
                                         @RequestParam(value = "obsList", required = false) String obsList, HttpServletRequest request) {
        Result result = new Result();
        List<String> obsCodeList = JSON.parseArray(obsList, String.class);
        UsersAuthority usersAuthority = new UsersAuthority();
        if(Common.isEmpty(usersId)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain() + ": 用户编码为空！");
            return result;
        }
        try {
            String insertName = tokenService.getUserName(request);
            usersAuthority.setInsertUser(insertName);
            usersAuthority.setUsersId(usersId);
            usersAuthority.setUserName(userName);
            usersAuthority.setAdminYn(adminYn);
            usersAuthority.setCheckYn(checkYn);
            result = usersAuthorityService.addMultiUsersAuthority(usersAuthority, obsCodeList, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }

    @ApiOperation(value = "新增用户权限接口", notes = "新增用户权限接口", httpMethod = "POST")
    @PostMapping(value = "/addUsersAuthority")
    public Result addUsersAuthority(@RequestBody UsersAuthority usersAuthority, HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(usersAuthority.getObsCode()) || Common.isEmpty(usersAuthority.getUsersId())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            String insertName = tokenService.getUserName(request);
            usersAuthority.setInsertUser(insertName);
            result = usersAuthorityService.addUsersAuthority(usersAuthority, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }


    @ApiOperation(value = "修改用户权限接口", notes = "修改用户权限接口", httpMethod = "PUT")
    @PutMapping(value = "/updateUsersAuthority")
    public Result updateUsersAuthority(@RequestBody UsersAuthority usersAuthority,HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(usersAuthority.getObsCode()) || Common.isEmpty(usersAuthority.getUsersId())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
            return result;
        }
        try {
            String insertName = tokenService.getUserName(request);
            usersAuthority.setModifyUser(insertName);
            result = usersAuthorityService.updateUsersAuthority(usersAuthority, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    @ApiOperation(value = "删除用户权限接口", notes = "删除用户权限接口", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteUsersAuthority")
    public Result deleteUsersAuthority(@RequestParam(value = "obsCode", required = true) String obsCode,
                                       @RequestParam(value = "usersId", required = true) String usersId,
                                       HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(obsCode) || Common.isEmpty(usersId)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain()+"|主键为空");
            return result;
        }
        try {
            result = usersAuthorityService.deleteUsersAuthority(obsCode, usersId, result);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
